package tr.edu.yildiz.ce.app_manager.ui.components.form;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import tr.edu.yildiz.ce.app_manager.model.AppProxy;
import tr.edu.yildiz.ce.app_manager.model.Application;
import tr.edu.yildiz.ce.app_manager.ui.AppManagerApplication;

import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Select;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;

public class ProxyForm extends BasicForm<AppProxy> {
    private Button addApplication = new Button("Add");
    TextField proxyName  = new TextField("Proxy Name");
    TextField proxyIp = new TextField("Proxy Ip Address");
    Select applicationSelect = new Select("Application"); 
    Select parentSelect = new Select("Parent");
    
    Table applicationTable = new Table();
    
    @Override
    protected void preSave() {
    	model.setProxyName(proxyName.getValue().toString());
    	model.setProxyIp(proxyIp.getValue().toString());
    	model.setParent((AppProxy) parentSelect.getValue());
    	List<Long> idList = new ArrayList<Long>(); 
    	idList.addAll((Collection<? extends Long>) applicationTable.getItemIds());
    	Set<Application> applicationSet = new HashSet<Application>();
    	for (Long id : idList) {
			applicationSet.add(app.getServiceProvider().getApplicationService().get(id));
		}
    	model.setApplications(applicationSet);
    }
    
	public ProxyForm(AppProxy appProxy,AppManagerApplication app) {
		super(app);
		model = appProxy;
		
		proxyName  = new TextField("Proxy Name");
	    proxyIp = new TextField("Proxy Ip Address");
	    
	    if (appProxy.getProxyId() != null){
	    	proxyName.setValue(appProxy.getProxyName());
	    	proxyIp.setValue(appProxy.getProxyIp());
	    }
	    
	    addField("Proxy Name", proxyName);
	    addField("Proxy Ip Address", proxyIp);
		
		initParentSelect(app);
		initApplicationSelect(app);
		initApplicationTable();
		initButtons();
		
	}

	private void initApplicationTable() {
		Set<Application> applications = model.getApplications();
		
		applicationTable.addContainerProperty("Application", String.class, null);
		refreshTable(applications);
		applicationTable.setSizeFull();
		applicationTable.addListener(new ItemClickListener() {
			@Override
			public void itemClick(ItemClickEvent event) {
//				proxyForm.setProxyName(event.getItemId().toString());
			}
		});
		applicationTable.setSelectable(true);
		addField("Applications", applicationTable);
	}

	private void refreshTable(Set<Application> applications) {
		if (applications != null)
			for (Application app : applications) {
				addApplication2Table(app);
			}
	}

	private void addApplication2Table(Application app) {
		applicationTable.addItem(new Object[]{app.getApplicationName()},app.getApplicationId());
	}

	private void initApplicationSelect(AppManagerApplication application) {
		List<Application> appList = application.getServiceProvider().getApplicationService().listAll();
		for (Application app : appList) {
			applicationSelect.addItem(app);
		}
		addField("Select Application", applicationSelect);
		addApplication.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				Application app = (Application)applicationSelect.getValue();
				if (model.getApplications() == null)
					model.setApplications(new HashSet<Application>());
				model.getApplications().add(app);
				refreshTable(model.getApplications());
			}
		});
		addField("", addApplication);
	}

	private void initParentSelect(AppManagerApplication application) {
		List<AppProxy> appList = application.getServiceProvider().getAppProxyService().listAll();
		for (AppProxy appProxy : appList) {
			if (model.getProxyId() == appProxy.getProxyId())
				continue;
			parentSelect.addItem(appProxy);
		}
		if (model.getParent() != null){
			parentSelect.select(model.getParent());
			parentSelect.setNullSelectionAllowed(false);
		}
		addField("Select Parent", parentSelect);
	}
	
	public void setProxyName(String proxyName) {
		this.proxyName.setValue(proxyName);
	}
	
	public void setProxyIp(String proxyIp) {
		this.proxyIp.setValue(proxyIp);
	}
}
